using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;
using System.Configuration;
using System.IO;

namespace MQRevo
{
    internal class Database
    {
        public string connectionString;
        public SqlConnection conn;
        public SqlCommand cmd;
        public string lastErr;

        public void initializeConn(){
            connectionString = ConfigurationManager.ConnectionStrings["MQ_Conn"].ConnectionString;
            conn = new SqlConnection(connectionString);
            conn.Open();
        }

        public SqlCommand MakeCommand_SP(string commandText)
        {
            try{
                cmd = new SqlCommand(commandText);
                cmd.Connection = conn;
                cmd.CommandType = System.Data.CommandType.StoredProcedure;
                return cmd;
            }
            catch (Exception ex){
                lastErr="[MakeCommand_SP]: "+ ex.InnerException;
                return null;
            }
        }

        public SqlCommand MakeCommand_SQL(string commandText)
        {
            try
            {
                cmd = new SqlCommand(commandText);
                cmd.Connection = conn;
                cmd.CommandType = System.Data.CommandType.Text;
                return cmd;
            }
            catch (Exception ex)
            {
                lastErr = "[MakeCommand_SQL]: " + ex.InnerException;
                return null;
            }
        }

        public void OpenConnection()
        {
            try{
                if (conn.State == System.Data.ConnectionState.Closed) conn.Open();
            }
            catch(Exception ex){
                lastErr = "[OpenConnection]: " + ex.InnerException;
            }
        }

        public void CloseConnection()
        {
            try
            {
                if (conn.State == System.Data.ConnectionState.Open) conn.Close();
            }
            catch (Exception ex)
            {
                lastErr = "[CloseConnection]: " + ex.InnerException;
            }
        }
    }
}
